package cn.luckday;

import cn.luckday.config.AppConfig;
import lombok.extern.slf4j.Slf4j;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

@Slf4j
public class Main {
    public static void main(String[] args) {
        int serverPort = Integer.valueOf(AppConfig.getProperty("port"));
        int numberOfThreads = Integer.valueOf(AppConfig.getProperty("poolNumber"));
        ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads);
        try (ServerSocket serverSocket = new ServerSocket(serverPort)) {
            log.info("The current service is listening on : {}", serverPort);

            while (true) {
                Socket client = serverSocket.accept();
                executor.submit(new ServiceHandler(client));
            }
        } catch (Exception ex) {
            log.error("Server exception: {}", ex.getMessage());
            ex.printStackTrace();
        } finally {
            log.info("server shutdown");
            executor.shutdown();
        }
    }
}
